package com.kuaixueit.crm.service.impl;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kuaixueit.crm.mapper.CFunctionMapper;
import com.kuaixueit.crm.pojo.CFunction;
import com.kuaixueit.crm.service.FunctionService;

@Service
public class FunctionServiceImpl implements FunctionService {
	
	@Autowired
	private CFunctionMapper functionMapper;


	@Override
	public int deleteByPrimaryKey(Integer id) {
		//根据删除节点的id查出来，当前节点的父结点
		CFunction node = functionMapper.selectByPrimaryKey(id);
		//用父结点id查询父节点下面的子节点
		int a = functionMapper.deleteByPrimaryKey(id);
		List<HashMap> nodes = functionMapper.findNodesByPid(Integer.valueOf(node.getPid()));
		//如果父结点没有子节点，修改父结点的genaratemenu为0
		if(nodes.size()==0) {
			CFunction pnode = functionMapper.selectByPrimaryKey(Integer.valueOf(node.getPid()));
			pnode.setGeneratemenu("0");
			functionMapper.updateByPrimaryKeySelective(pnode);
		}
		return a;
	}

	@Override
	public int insert(CFunction record) {
		CFunction pnode = functionMapper.selectByPrimaryKey(Integer.valueOf(record.getPid()));
		pnode.setGeneratemenu("1");
		functionMapper.updateByPrimaryKeySelective(pnode);
		record.setId(null);
		record.setGeneratemenu("0");
		return functionMapper.insert(record);
	}

	@Override
	public int insertSelective(CFunction record) {
		record.setGeneratemenu("0");
		return functionMapper.insertSelective(record);
	}

	@Override
	public CFunction selectByPrimaryKey(Integer id) {
		return functionMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(CFunction record) {
		return functionMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(CFunction record) {
		return functionMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<HashMap> findNodesByPid(Integer pid) {
		return functionMapper.findNodesByPid(pid);
	}

}
